package com.uflower.order.vo;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;


public class CartVo {
    /**
     * 购物项集合
     */
    List<CartItemVo> items;
    /**
     * 勾选商品总数量
     */
    private Integer countNum = 0;

    /**
     * 商品类型总数量
     */
    private Integer countType = 0;

    /**
     * 总优惠价
     */
    private BigDecimal reduce = new BigDecimal("0.00");

    /**
     * 商品总价
     */

    private BigDecimal totalAmount = new BigDecimal("0.00");

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public Integer getCountNum(List<CartItemVo> items) {
        int count = 0;
        if(items != null && items.size() >0 )
        for (CartItemVo item : items) {
            if(item.getCheck()){
                count += item.getCount();
            }
        }
        return count;
    }
    public Integer getCountType() {
        int countType = 0;
        if(items != null && items.size() >0) {
            countType = items.size();
        }
        return countType;
    }

    public BigDecimal getReduce() {
        BigDecimal reduce = new BigDecimal("0");
        if(items != null && items.size() >0 )
            for (CartItemVo item : items) {
                if(item.getCheck()){
                    CartItemVo.FullReduce fullReduce = item.getFullReduce();
                    if(fullReduce != null){
                        if(fullReduce.getFullPrice().compareTo(totalAmount) == -1){
                            reduce  =   reduce.add(fullReduce.getReducePrice());
                        }
                    }
                }
            }
        return reduce;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        if(items != null && items.size() >0 )
            for (CartItemVo item : items) {
                if(item.getCheck()){
                  amount =  amount.add(item.getTotalPrice());
                }
            }
        return amount;
    }
    public void init(){
        countNum = getCountNum(items);
        countType = getCountType();
        totalAmount = getTotalAmount();
        reduce = getReduce();

    }

}
